﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.ComponentModel;

namespace SmallTfs
{

    public static class StaticConfManager
    {
        public static ConfManager CManager {get;set;}
    }

    public class ConfManager  
    {
        Configuration configuration = null;

        public Configuration Configuration
        {
            get
            {
                if (configuration == null)
                    LoadConfiguration();

                return configuration; 
            }
            set
            {
                configuration = value;
               
            }
        }

        private void LoadConfiguration()
        {
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();
                
                if (!File.Exists("Conf.cfg"))
                {
                    GenerateDefaultConfiguration();

                    SaveConfiguration(formatter);
                }
                else
                {
                    FileStream fs = new FileStream("Conf.cfg", FileMode.Open);
                    configuration = (Configuration)formatter.Deserialize(fs);
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                GenerateDefaultConfiguration();
            }
        }

        private void SaveConfiguration(BinaryFormatter formatter)
        {
            try
            {
                FileStream fs = new FileStream("Conf.cfg", FileMode.Create);
                formatter.Serialize(fs, configuration);
                fs.Close();
            }
            catch (Exception ex)
            {
            }
        }

        private void GenerateDefaultConfiguration()
        {
            configuration = new Configuration();
            configuration.LocalPath = @"c:\";
            configuration.ProjectName = "$/dev";
            configuration.ServerName = @"http://srvtfs:8080/tfs/ispc dev";
            configuration.WorkspaceName = Environment.MachineName;
        }

        public string ServerName
        {
            get { return Configuration.ServerName; }
            set
            {
                Configuration.ServerName = value;
                BinaryFormatter formatter = new BinaryFormatter();
                SaveConfiguration(formatter);
            }
        }


        public string ProjectName
        {
            get { return Configuration.ProjectName; }
            set
            {
                Configuration.ProjectName = value;
                BinaryFormatter formatter = new BinaryFormatter();
                SaveConfiguration(formatter);
            }
        }


        public string WorkspaceName
        {
            get { return Configuration.WorkspaceName; }
            set
            {
                Configuration.WorkspaceName = value;
                BinaryFormatter formatter = new BinaryFormatter();
                SaveConfiguration(formatter);
            }
        }


        public string RootMergePath
        {
            get
            {
                return Configuration.RootMergePath; 
            }
            set
            {

                Configuration.RootMergePath = value;
                BinaryFormatter formatter = new BinaryFormatter();
                SaveConfiguration(formatter);
            }

        }


        public string LocalPath
        {
            get { return Configuration.LocalPath; }
            set
            {
                Configuration.LocalPath = value;
                BinaryFormatter formatter = new BinaryFormatter();
                SaveConfiguration(formatter);
            }
        }
    }
}
